package com.example.socialwebserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.socialwebserver.mapper.LoginMapper;
import com.example.socialwebserver.pojo.*;
import com.example.socialwebserver.service.ExpectFriendService;
import com.example.socialwebserver.service.ExpectLoveService;
import com.example.socialwebserver.service.LoginService;
import com.example.socialwebserver.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    LoginMapper loginMapper;
    @Autowired
    ExpectFriendService expectFriendService;
    @Autowired
    ExpectLoveService expectLoveService;
    @Autowired
    UserService userService;

    @Override
    public Integer Login(LoginDTO userDTO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name", userDTO.getUserName());
        queryWrapper.eq("password", userDTO.getPassword());
        LoginDAO loginDAO = loginMapper.selectOne(queryWrapper);
        if(loginDAO==null)
        {
            return -1;
        }
        return loginDAO.getId();
    }
    @Override
    public LoginDAO SelectById(Integer id) {
        return loginMapper.selectById(id);
    }
    @Override
    public boolean Register(LoginDTO loginDTO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name",loginDTO.getUserName());
        LoginDAO loginDAO = loginMapper.selectOne(queryWrapper);//查询数据库中是否存在相同的用户名
        if(loginDAO == null) {//如果不存在注册
            LoginDAO loginDAO1 = new LoginDAO();
            BeanUtils.copyProperties(loginDTO,loginDAO1);
            int insertRow = loginMapper.insert(loginDAO1);
            if (insertRow == 0) {
                return false;//如果注册插入失败则返回false
            } else {
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("user_name", loginDTO.getUserName());
                LoginDAO loginDAO2 = loginMapper.selectOne(queryWrapper1);//查询刚刚插入的用户的id
                UserDAO userDAO = new UserDAO();//将注册的用户id插入user表
                userDAO.setId(loginDAO2.getId());

                ExpectFriendDAO expectFriendDAO = new ExpectFriendDAO();
                expectFriendDAO.setId(loginDAO2.getId());

                ExpectLoveDAO expectLoveDAO = new ExpectLoveDAO();
                expectLoveDAO.setId(loginDAO2.getId());

                if(expectLoveService.InsertExLoDAO(expectLoveDAO)&&
                        expectFriendService.InsertExFrDAO(expectFriendDAO)&&
                                userService.InsertUser(userDAO)){ return true; }
                else{
                    return false;
                }
            }
        }else {
            return false;
        }
    }
    @Override
    public boolean UpdateById(LoginDAO loginDAO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",loginDAO.getId());
        return loginMapper.update(loginDAO,queryWrapper) == 1?true:false;
    }

    @Override
    public boolean DeleteById(Integer id) {
        return loginMapper.deleteById(id) == 1?true:false;
    }
    @Override
    public boolean ChangePassword(LoginPasswordDTO loginPasswordDTO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", loginPasswordDTO.getId());
        queryWrapper.eq("password", loginPasswordDTO.getOldPassword());
        LoginDAO loginDAO = new LoginDAO();
        loginDAO.setPassword(loginPasswordDTO.getNewPassword());
        int updateRow = loginMapper.update(loginDAO, queryWrapper);
        return updateRow >= 1 ? true : false;
    }
}
